package cz.muni.fi.pa165.service.local.impl;

import cz.muni.fi.pa165.transfers.CustomerT;
import cz.muni.fi.pa165.transfers.DeliveryT;
import cz.muni.fi.pa165.dao.DeliveryDAO;
import cz.muni.fi.pa165.dto.CustomerDTO;
import cz.muni.fi.pa165.dto.DeliveryDTO;
import cz.muni.fi.pa165.dto.EmployeeDTO;
import cz.muni.fi.pa165.entity.Delivery;
import cz.muni.fi.pa165.entity.Employee;
import cz.muni.fi.pa165.transfers.EmployeeT;
import cz.muni.fi.pa165.service.local.DeliveryServiceLocal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
/**
 *
 * @author Ondrej Cernay
 */
@Stateless
public class DeliveryService implements DeliveryServiceLocal {

    @EJB
    private DeliveryDAO deliveryDAO;
    
    public Long createDelivery(DeliveryDTO delivery) {
        Delivery entity = DeliveryT.dTOtoEntity(delivery);
        deliveryDAO.createDelivery(entity);
        return entity.getId();
    }

    public void updateDelivery(DeliveryDTO delivery) {
        deliveryDAO.updateDelivery(DeliveryT.dTOtoEntity(delivery));
    }

    public void removeDelivery(DeliveryDTO delivery) {
        deliveryDAO.removeDelivery(DeliveryT.dTOtoEntity(delivery));
    }

    public List<DeliveryDTO> getAllDeliveries() {
        List<Delivery> deliveries = deliveryDAO.getAllDeliveries();
        List<DeliveryDTO> deliveriesDTO = new ArrayList<DeliveryDTO>();
        for(Delivery delivery : deliveries) {
            deliveriesDTO.add(DeliveryT.entityToDTO(delivery));
        }
        return deliveriesDTO;
    }

    public List<DeliveryDTO> getDeliveriesByDeliveryBoy(EmployeeDTO deliveryBoy) {
        List<Delivery> deliveries = deliveryDAO.getDeliveriesByDeliveryBoy(EmployeeT.dTOtoEntity(deliveryBoy));
        List<DeliveryDTO> deliveriesDTO = new ArrayList<DeliveryDTO>();
        for(Delivery delivery : deliveries) {
            deliveriesDTO.add(DeliveryT.entityToDTO(delivery));
        }
        return deliveriesDTO;
    }

    public List<DeliveryDTO> getDeliveriesByCustomer(CustomerDTO customer) {
        List<Delivery> deliveries = deliveryDAO.getDeliveriesByCustomer(CustomerT.dTOtoEntity(customer));
        List<DeliveryDTO> deliveriesDTO = new ArrayList<DeliveryDTO>();
        for(Delivery delivery : deliveries) {
            deliveriesDTO.add(DeliveryT.entityToDTO(delivery));
        }
        return deliveriesDTO;
    }

    public List<DeliveryDTO> getDeliveriesByDeliveryDate(Date from, Date to) {
        List<Delivery> deliveries = deliveryDAO.getDeliveriesByDeliveryDate(from, to);
        List<DeliveryDTO> deliveriesDTO = new ArrayList<DeliveryDTO>();
        for(Delivery delivery : deliveries) {
            deliveriesDTO.add(DeliveryT.entityToDTO(delivery));
        }
        return deliveriesDTO;
    }

    public List<DeliveryDTO> getDeliveriesByCreationDate(Date from, Date to) {
        List<Delivery> deliveries = deliveryDAO.getDeliveriesByCreationDate(from, to);
        List<DeliveryDTO> deliveriesDTO = new ArrayList<DeliveryDTO>();
        for(Delivery delivery : deliveries) {
            deliveriesDTO.add(DeliveryT.entityToDTO(delivery));
        }
        return deliveriesDTO;
    }

    public List<DeliveryDTO> getDeliveriesByRecipient(String name) {
        List<Delivery> deliveries = deliveryDAO.getDeliveriesByRecipient(name);
        List<DeliveryDTO> deliveriesDTO = new ArrayList<DeliveryDTO>();
        for(Delivery delivery : deliveries) {
            deliveriesDTO.add(DeliveryT.entityToDTO(delivery));
        }
        return deliveriesDTO;
    }

    public List<DeliveryDTO> getDeliveriesByAddress(String address) {
        List<Delivery> deliveries = deliveryDAO.getDeliveriesByAddress(address);
        List<DeliveryDTO> deliveriesDTO = new ArrayList<DeliveryDTO>();
        for(Delivery delivery : deliveries) {
            deliveriesDTO.add(DeliveryT.entityToDTO(delivery));
        }
        return deliveriesDTO;        
    }

    public List<DeliveryDTO> getDeliveriesByZip(String zip) {
        List<Delivery> deliveries = deliveryDAO.getDeliveriesByZip(zip);
        List<DeliveryDTO> deliveriesDTO = new ArrayList<DeliveryDTO>();
        for(Delivery delivery : deliveries) {
            deliveriesDTO.add(DeliveryT.entityToDTO(delivery));
        }
        return deliveriesDTO; 
    }

    public List<DeliveryDTO> getDeliveriesByCity(String city) {
        List<Delivery> deliveries = deliveryDAO.getDeliveriesByCity(city);
        List<DeliveryDTO> deliveriesDTO = new ArrayList<DeliveryDTO>();
        for(Delivery delivery : deliveries) {
            deliveriesDTO.add(DeliveryT.entityToDTO(delivery));
        }
        return deliveriesDTO;         
    }

    public DeliveryDTO getDeliveryById(Long id) {
        return DeliveryT.entityToDTO(deliveryDAO.getDeliveryById(id));
    }
    
}
